/*

********************************************
*  Mind+ blocks for PhyphoxBLE liberary  *
7*                 version V0.0.1                          *
*   (based on Phyphox liberary 1.0.6)         *
********************************************
hardware: ESP32s, moble phone with phyphox
            software: phyphox>=1.1.7
               Designed by shytears, 
            first released at 7/22/2021
two usage:
1. use "create simple experiment" for one graph, one view experiment
2. use create experiment through experiment, view, graph.every experiment could have more than one view, and every view could have more than one graph.

notice:
because of the special type of param in phyphoxexperiment::write(float&), datas(which is float type) that wants to "write" to phyphox, should follow instructions below:
1. drop "set value" block as "setup" code to create a variant of float type.
2. drop "set value" block in "loop" , use the block to store income data into "value".
3. drop "get value" block where needs the imcome data. 
*/

//% color="#AA278D" iconWidth=50 iconHeight=40
namespace phyphox {
    
    let ESP="esp";
    let VIEW="view_";
    let GRAPH="graph_";    

    //% block="phyphox-START [BLE]" blockType="command"
    //% BLE.shadow="string" BLE.defl=BLE_name
    export function phySTART(parameter: any, block: any) {
        let ble = parameter.BLE.code
        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addCode(`PhyphoxBLE::start(${ble});`);
    }

    //% block="Create simple Experiment with title[TIT1], LableY [LY1], UnitY [UY1] , LableX [LX1] , UnitX [UX1]" blockType="command"  
    //% TIT1.shadow="string" TIT1.defl=title_name
    //% UX1.shadow="string" UX1.defl=X_axis_unit
    //% UY1.shadow="string" UY1.defl=Y_axis_unit
    //% LX1.shadow="string" LX1.defl=X_axis_lable
    //% LY1.shadow="string" LY1.defl=Y_axis_lable

    export function create_simple_exp(parameter: any, block: any) {
        let tit1 = parameter.TIT1.code
        let ux1 = parameter.UX1.code
        let uy1 = parameter.UY1.code
        let lx1 = parameter.LX1.code
        let ly1 = parameter.LY1.code

        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addObject(`${ESP}`, `PhyphoxBleExperiment`, `${ESP};`);
        //Generator.addObject(`view_1`, `PhyphoxBleExperiment::View`, `view_1;`);
        //Generator.addObject(`graph_1`, `PhyphoxBleExperiment::Graph, `graph_1;`);
        Generator.addCode(`${ESP}.setTitle(${tit1});\n\t${ESP}.setCategory("ESP32_Arduino");\n\t${ESP}.setDescription("simple experiment");\n\tPhyphoxBleExperiment::View view_1;\n\tview_1.setLabel("Rawdata");\n\tPhyphoxBleExperiment::Graph graph_1;\n\tgraph_1.setLabel("simple graph");\n\tgraph_1.setUnitX(${ux1});\n\tgraph_1.setUnitY(${uy1});\n\tgraph_1.setLabelX(${lx1});\n\tgraph_1.setLabelY(${ly1});\n\tgraph_1.setChannel(0,1);\n\tview_1.addElement(graph_1);\n\t${ESP}.addView(view_1);`);
    }

    ///% block="set value [V]" blockType="command"
    //% V.shadow="number" V.defl=0 
    export function def_value(parameter: any, block: any) {
        let v=parameter.V.code
        Generator.addObject(`value`, `float `, `value;`);
        Generator.addCode(`value =${v};`);
    }

    //% block="get value" blockType="reporter"
    export function get_value(parameter: any, block: any) {
        Generator.addCode(`value`);
    } 

    //% block="phyphox-Write [NUM]" blockType="command"
    //% NUM.shadow="number" 
    export function phyWrite(parameter: any, block: any) {
        let num= parameter.NUM.code
        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addCode(`PhyphoxBLE::write(${num});`);
    }    

    //% block=" Add Exp to Phyphox " blockType="command"
    export function add_ESP(parameter: any, block: any) {
        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addCode(`PhyphoxBLE::addExperiment(${ESP});`);
    }

    //% block="Create Experiment with title[TIT],Category[CAT],Description[DES]" blockType="command"  
    //% TIT.shadow="string" TIT.defl=title_name
    //% CAT.shadow="string" CAT.defl=ESP32_Arduino
    //% DES.shadow="string" DES.defl=Experiment_description
    export function create_exp(parameter: any, block: any) {
        let tit = parameter.TIT.code
        let cat = parameter.CAT.code
        let des = parameter.DES.code
        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addObject(`${ESP}`, `PhyphoxBleExperiment`, `${ESP};`);
        Generator.addCode(`${ESP}.setTitle(${tit});\n\t${ESP}.setCategory(${cat});\n\t${ESP}.setDescription(${des});`);
    }

    //% block="Create View [V_COUNT] with lable[V_LABLE]" blockType="command"
    //% V_COUNT.shadow="number" V_COUNT.defl="1" 
    //% V_LABLE.shadow="string" V_LABLE.defl=view_lable
  
    export function create_view(parameter: any, block: any) {
        let v_count = parameter.V_COUNT.code
        let v_lable = parameter.V_LABLE.code

        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addObject(`${VIEW}${v_count}`, `PhyphoxBleExperiment::View`, `${VIEW}${v_count};`);
        Generator.addCode(`${VIEW}${v_count}.setLabel("Rawdata");`);
    }

    //% block="Create Graph [G_COUNT] for view with Lable [G_LABLE] ,LableY [LY], UnitY [UY] , LableX [LX] , UnitX [UX]" blockType="command"
    //% G_COUNT.shadow="number" G_COUNT.defl="1"
    //% G_LABLE.shadow="string" G_LABLE.defl=graph_name
    //% UX.shadow="string" UX.defl=X_axis_unit
    //% UY.shadow="string" UY.defl=Y_axis_unit
    //% LX.shadow="string" LX.defl=X_axis_lable
    //% LY.shadow="string" LY.defl=Y_axis_lable
    export function create_graph(parameter: any, block: any) {
        let g_count = parameter.G_COUNT.code
        let g_lable = parameter.G_LABLE.code
        let ux = parameter.UX.code
        let uy = parameter.UY.code
        let lx = parameter.LX.code
        let ly = parameter.LY.code
        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addObject(`${GRAPH}${g_count}`, `PhyphoxBleExperiment::Graph`, `${GRAPH}${g_count};`);
        Generator.addCode(`${GRAPH}${g_count}.setLabel(${g_lable});\n\t${GRAPH}${g_count}.setUnitX(${ux});\n\t${GRAPH}${g_count}.setUnitY(${uy});\n\t${GRAPH}${g_count}.setLabelX(${lx});\n\t${GRAPH}${g_count}.setLabelY(${ly});\n\t${GRAPH}${g_count}.setChannel(0,1);`);
    }
    
    //% block=" Add Graph [G_COUNT] to View [V_COUNT] as Element" blockType="command"
    //% V_COUNT.shadow="number" G_COUNT.defl=-1
    //% G_COUNT.shadow="number" V_COUNT.defl=-1
    export function add_graph(parameter: any, block: any) {
        let v_count= parameter.V_COUNT.code
        let g_count= parameter.G_COUNT.code
        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addCode(`${VIEW}${v_count}.addElement(${GRAPH}${g_count});`);
   }
    //% block=" Add View [V_COUNT] to Experiment " blockType="command"
    //% V_COUNT.shadow="number" G_COUNT.defl=-1
    //% G_COUNT.shadow="number" V_COUNT.defl=-1
    export function add_view(parameter: any, block: any) {
        let v_count= parameter.V_COUNT.code

        Generator.addInclude('phyphoxBLE', '#include <phyphoxBle.h>');
        Generator.addCode(`${ESP}.addView(${VIEW}${v_count});`);
    }

}
